<template>
  <div class="register">
    <div class="register-box">
      <h2>选择头像</h2>
      <!-- <div class="head">
        <img :src="url" />
      </div>-->
      <el-upload
        class="avatar-uploader"
        action="/api/CjwAdmin/UploadPictures"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="用户名">
          <el-input v-model="form.adminName"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="form.adminPassword"></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="form.phoneNumber"></el-input>
        </el-form-item>
        <el-form-item label="身份证号码">
          <el-input v-model="form.idNumber"></el-input>
        </el-form-item>
        <el-form-item label="出生日期">
          <el-date-picker v-model="form.birth" type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio v-model="form.sex" label="0">女</el-radio>
          <el-radio v-model="form.sex" label="1">男</el-radio>
        </el-form-item>
        <el-form-item label="职位">
          <el-select v-model="form.position" placeholder="请选择">
            <el-option
              v-for="item in positions"
              :key="item.positionId"
              :label="item.positionName"
              :value="item.positionName"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="formSubmit">注册</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: "register",
  props: {
    adminName: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      imageUrl: "",
      url: "",
      form: {
        adminName: "",
        adminPassword: "",
        phoneNumber: "",
        idNumber: "",
        birth: "",
        sex: "",
        position: "",
        state: 1
      },
      positions: [
        {
          value: "1",
          label: "前台管理"
        },
        {
          value: "2",
          label: "护士"
        },
        {
          value: "3",
          label: "医生"
        }
      ]
    };
  },
  created() {
    this.getPositionList();
  },
  methods: {
    formSubmit() {
      let params = {
        adminName: this.form.adminName,
        adminPassword: this.form.adminPassword,
        phoneNumber: this.form.phoneNumber,
        idNumber: this.form.idNumber,
        birth: this.form.birth,
        sex: this.form.sex,
        position: this.form.position,
        state: 1,
        img: this.url
      };
      console.log(params);
      this.$axios
        .post("/api/CjwAdmin/AdminRegister", params)
        .then(res => {
          if (res.data) {
            this.$message({
              message: "注册成功",
              type: "success"
            });
            let op = {
              opAdminName: this.adminName,
              opTable: "员工注册",
              opData: `帐号:${params.adminName}职位：${params.position}`
            };
            this.$axios.post("/api/CjwOperationList/InsertOpData", op);
            this.$router.push("/accountManage");
          } else {
            this.$message.error("注册失败,已存在用户名");
          }
        })
        .catch(res => {
          this.$message.error("网络异常");
        });

      // console.log(this.url);
    },
    getPositionList() {
      this.$axios.get("/api/CjwPosition/GetPositionList").then(res => {
        this.positions = res.data;
      });
    },
    handleAvatarSuccess(res, file) {
      // console.log(res);
      // console.log(file);
      this.imageUrl = URL.createObjectURL(file.raw);
      this.url = res;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    }
  }
};
</script>

<style scoped>
.register-box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.register-box .el-form {
  padding-right: 50px;
  width: 400px;
}

.register-box .el-form .el-select {
  width: 100%;
}

.register-box .el-form .el-date-editor {
  width: 100%;
}

.register-box .el-button {
  width: 250px;
  padding-left: 20px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.head img {
  width: 140px;
  height: 140px;
}
.editHead {
  padding-left: 20px;
}
</style>
